<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('菜单列表')"/>
</head>
<body class="pear-container">
<div sec:authorize="hasPermission('menu','list')" class="layui-card">
    <div class="layui-card-body">
        <table id="menu-table" lay-filter="menu-table"></table>
    </div>
</div>
<div sec:authorize="not hasPermission('menu','list')" class="layui-card">
    <div class="layui-card-body">
        <b>您无权查看该列表</b>
    </div>
</div>
<script id="menu-toolbar" type="text/html">
    <!--    <button
                class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>-->
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script id="menu-bar" type="text/html">
    <button
            class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button
            class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script id="menu-type" type="text/html">
    {{#if (d.type == '0') { }}
    <span>目录</span>
    {{# }else if(d.type == '1'){ }}
    <span>菜单</span>
    {{# }else{ }}
    <span>未知</span>
    {{# } }}
</script>

<script id="open-type" type="text/html">
    {{#if (d.openType == '_iframe') { }}
    <span>框架</span>
    {{# }else if(d.openType == '0'){ }}
    <span>标签页</span>
    {{# }else{ }}
    <span>--</span>
    {{# } }}
</script>

<script id="icon" type="text/html">
    <i class="layui-icon {{d.icon}}"></i>
</script>

<script id="menu-href" type="text/html">
    {{#if (d.href == null||d.href=='') { }}
    <span>--</span>
    {{# }else{ }}
    <button plain class="pear-btn pear-btn-success">{{d.href}}</button>
    {{# } }}
</script>
</body>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treeTable = layui.treetable;
        let popup = layui.popup;
         // 判断权限
        if ([[${#authorization.expression("hasPermission('menu','list')")}]]) {
            treeTable.render({
                treeColIndex: 0,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'parentId',
                skin: 'line',
                method: 'get',
                treeDefaultClose: true,
                toolbar: '#menu-toolbar',
                elem: '#menu-table',
                url: '/system/menu/list',
                page: false,
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                    title: '刷新'
                }, 'filter', 'print', 'exports'],
                cols: [
                    [
                        // {type: 'checkbox'},
                        {field: 'title', minWidth: 200, title: '菜单名称'},
                        {field: 'type', minWidth: 200, templet: '#menu-type', title: '类型'},
                        {field: 'icon', title: '图标', templet: '#icon'},
                        {field: 'openType', title: '打开类型', templet: '#open-type'},
                        {field: 'href', minWidth: 200, title: '链接', templet: '#menu-href'},
                        // {title: '操作', templet: '#menu-bar', width: 150, align: 'center'}
                    ]
                ]
            });
        }

        form.on('submit(menu-query)', function (data) {
            treeTable.search("#menu-table", data.field.powerName)
            return false;
        });

        table.on('tool(menu-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })

        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'expandAll') {
                treeTable.expandAll("#menu-table");
            } else if (obj.event === 'foldAll') {
                treeTable.foldAll("#menu-table");
            }
        });

        form.on('switch(menu-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "enable";
            } else {
                operate = "disable";
            }
            let loading = layer.load();
            $.ajax({
                url: '/system/menu/' + operate,
                data: JSON.stringify({powerId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        popup.success(result.msg);
                    } else {
                        popup.failure(result.msg);
                    }
                }
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.5,
                area: ['700px', '500px'],
                content: '/system/menu/add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.5,
                area: ['700px', '500px'],
                content: '/system/menu/edit?powerId=' + obj.data['powerId']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该菜单', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/system/menu/remove/" + obj.data['powerId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            popup.success(result.msg, function () {
                                obj.del();
                            })
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            treeTable.reload('#menu-table');
        }
    })
</script>
</html>